package archimede;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.util.NoSuchElementException;
import java.util.Scanner;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;

import net.miginfocom.swing.MigLayout;

public class ProfessionistaObject extends JPanel {

	private int count = 0;
	private JTextField nome = new JTextField() {
		@Override
		protected void paintComponent(Graphics g) {
			if (!isOpaque()) {
				int w = getWidth();
				int h = getHeight();
				Graphics2D g2 = (Graphics2D) g.create();
				g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
						RenderingHints.VALUE_ANTIALIAS_ON);
				g2.setColor(UIManager.getColor("TextField.background"));
				g2.fillRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.setColor(Color.BLACK);
				g2.drawRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.dispose();
			}
			super.paintComponent(g);
		}
	};
	private JTextField cognome = new JTextField() {
		@Override
		protected void paintComponent(Graphics g) {
			if (!isOpaque()) {
				int w = getWidth();
				int h = getHeight();
				Graphics2D g2 = (Graphics2D) g.create();
				g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
						RenderingHints.VALUE_ANTIALIAS_ON);
				g2.setColor(UIManager.getColor("TextField.background"));
				g2.fillRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.setColor(Color.BLACK);
				g2.drawRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.dispose();
			}
			super.paintComponent(g);
		}
	};
	private JTextField via = new JTextField() {
		@Override
		protected void paintComponent(Graphics g) {
			if (!isOpaque()) {
				int w = getWidth();
				int h = getHeight();
				Graphics2D g2 = (Graphics2D) g.create();
				g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
						RenderingHints.VALUE_ANTIALIAS_ON);
				g2.setColor(UIManager.getColor("TextField.background"));
				g2.fillRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.setColor(Color.BLACK);
				g2.drawRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.dispose();
			}
			super.paintComponent(g);
		}
	};
	private JTextField civico = new JTextField() {
		@Override
		protected void paintComponent(Graphics g) {
			if (!isOpaque()) {
				int w = getWidth();
				int h = getHeight();
				Graphics2D g2 = (Graphics2D) g.create();
				g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
						RenderingHints.VALUE_ANTIALIAS_ON);
				g2.setColor(UIManager.getColor("TextField.background"));
				g2.fillRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.setColor(Color.BLACK);
				g2.drawRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.dispose();
			}
			super.paintComponent(g);
		}
	};
	private JTextField citta = new JTextField() {
		@Override
		protected void paintComponent(Graphics g) {
			if (!isOpaque()) {
				int w = getWidth();
				int h = getHeight();
				Graphics2D g2 = (Graphics2D) g.create();
				g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
						RenderingHints.VALUE_ANTIALIAS_ON);
				g2.setColor(UIManager.getColor("TextField.background"));
				g2.fillRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.setColor(Color.BLACK);
				g2.drawRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.dispose();
			}
			super.paintComponent(g);
		}
	};
	private JTextField provincia = new JTextField() {
		@Override
		protected void paintComponent(Graphics g) {
			if (!isOpaque()) {
				int w = getWidth();
				int h = getHeight();
				Graphics2D g2 = (Graphics2D) g.create();
				g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
						RenderingHints.VALUE_ANTIALIAS_ON);
				g2.setColor(UIManager.getColor("TextField.background"));
				g2.fillRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.setColor(Color.BLACK);
				g2.drawRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.dispose();
			}
			super.paintComponent(g);
		}
	};
	private JTextField cap = new JTextField() {
		@Override
		protected void paintComponent(Graphics g) {
			if (!isOpaque()) {
				int w = getWidth();
				int h = getHeight();
				Graphics2D g2 = (Graphics2D) g.create();
				g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
						RenderingHints.VALUE_ANTIALIAS_ON);
				g2.setColor(UIManager.getColor("TextField.background"));
				g2.fillRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.setColor(Color.BLACK);
				g2.drawRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.dispose();
			}
			super.paintComponent(g);
		}
	};
	private JTextField cell = new JTextField() {
		@Override
		protected void paintComponent(Graphics g) {
			if (!isOpaque()) {
				int w = getWidth();
				int h = getHeight();
				Graphics2D g2 = (Graphics2D) g.create();
				g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
						RenderingHints.VALUE_ANTIALIAS_ON);
				g2.setColor(UIManager.getColor("TextField.background"));
				g2.fillRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.setColor(Color.BLACK);
				g2.drawRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.dispose();
			}
			super.paintComponent(g);
		}
	};
	private JTextField tel = new JTextField() {
		@Override
		protected void paintComponent(Graphics g) {
			if (!isOpaque()) {
				int w = getWidth();
				int h = getHeight();
				Graphics2D g2 = (Graphics2D) g.create();
				g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
						RenderingHints.VALUE_ANTIALIAS_ON);
				g2.setColor(UIManager.getColor("TextField.background"));
				g2.fillRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.setColor(Color.BLACK);
				g2.drawRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.dispose();
			}
			super.paintComponent(g);
		}
	};
	private JTextField mail = new JTextField() {
		@Override
		protected void paintComponent(Graphics g) {
			if (!isOpaque()) {
				int w = getWidth();
				int h = getHeight();
				Graphics2D g2 = (Graphics2D) g.create();
				g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
						RenderingHints.VALUE_ANTIALIAS_ON);
				g2.setColor(UIManager.getColor("TextField.background"));
				g2.fillRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.setColor(Color.BLACK);
				g2.drawRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.dispose();
			}
			super.paintComponent(g);
		}
	};

	private JTextField tipoProfessionista = new JTextField() {
		@Override
		protected void paintComponent(Graphics g) {
			if (!isOpaque()) {
				int w = getWidth();
				int h = getHeight();
				Graphics2D g2 = (Graphics2D) g.create();
				g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
						RenderingHints.VALUE_ANTIALIAS_ON);
				g2.setColor(UIManager.getColor("TextField.background"));
				g2.fillRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.setColor(Color.BLACK);
				g2.drawRoundRect(0, 0, w - 2, h - 2, 10, 10);
				g2.dispose();
			}
			super.paintComponent(g);
		}
	};

	private ProfessionistaPanel professionista;

	public ProfessionistaObject(int i, ProfessionistaPanel m) {
		super();
		count = i;
		professionista = m;
		setLayout(new MigLayout());
		setOpaque(false);

		setCell();

		JPanel panelDati = new JPanel();
		panelDati.setLayout(new MigLayout());
		panelDati.setOpaque(false);

		panelDati.add(new JLabel("Titolo :") {
			@Override
			public void setFont(Font font) {
				// TODO Auto-generated method stub
				super.setFont(new Font("Arial", Font.BOLD, 16));
			}
		});
		panelDati.add(tipoProfessionista);
		
		add(panelDati,"dock north");
		
		add(new JLabel("Nome  ") {
			@Override
			public void setFont(Font font) {
				// TODO Auto-generated method stub
				super.setFont(new Font("Arial", Font.BOLD, 16));
			}
		}, "cell 0 0");
		add(nome, "cell 1 0 ");
		add(new JLabel("Cognome  ") {
			@Override
			public void setFont(Font font) {
				// TODO Auto-generated method stub
				super.setFont(new Font("Arial", Font.BOLD, 16));
			}
		}, "cell 2 0");
		add(cognome, "cell 3 0 3 1");
		add(new JLabel("Via  ") {
			@Override
			public void setFont(Font font) {
				// TODO Auto-generated method stub
				super.setFont(new Font("Arial", Font.BOLD, 16));
			}
		}, "cell 0 1");
		add(via, "cell 1 1 ");
		add(new JLabel("Civico") {
			@Override
			public void setFont(Font font) {
				// TODO Auto-generated method stub
				super.setFont(new Font("Arial", Font.BOLD, 16));
			}
		}, "cell 2 1");
		add(civico, "cell 3 1");
		add(new JLabel("Citta'  ") {
			@Override
			public void setFont(Font font) {
				// TODO Auto-generated method stub
				super.setFont(new Font("Arial", Font.BOLD, 16));
			}
		}, "cell 0  2 ");
		add(citta, "cell 1 2");
		add(new JLabel("CAP") {
			@Override
			public void setFont(Font font) {
				// TODO Auto-generated method stub
				super.setFont(new Font("Arial", Font.BOLD, 16));
			}
		}, "cell 2  2 ");
		add(cap, "cell 3 2");
		add(new JLabel("Provincia  ") {
			@Override
			public void setFont(Font font) {
				// TODO Auto-generated method stub
				super.setFont(new Font("Arial", Font.BOLD, 16));
			}
		}, "cell 4  2 , split 2");
		add(provincia, "cell 4 2");
		add(new JLabel("E-Mail  ") {
			@Override
			public void setFont(Font font) {
				// TODO Auto-generated method stub
				super.setFont(new Font("Arial", Font.BOLD, 16));
			}
		}, "cell 0  3 ");
		add(mail, "cell 1 3");

		add(new JLabel("Cell.") {
			@Override
			public void setFont(Font font) {
				// TODO Auto-generated method stub
				super.setFont(new Font("Arial", Font.BOLD, 16));
			}
		}, "cell 0  4 ");
		add(cell, "cell 1 4");

		add(new JLabel("Tel.") {
			@Override
			public void setFont(Font font) {
				// TODO Auto-generated method stub
				super.setFont(new Font("Arial", Font.BOLD, 16));
			}
		}, "cell 0  5 ");
		add(tel, "cell 1 5");
	}

	public void remove() {
		professionista.removeTabel(count);
	}

	public void setCount(int i) {
		count = i;
	}

	public void setCell() {
		nome.setPreferredSize(new Dimension(300, 35));
		cognome.setPreferredSize(new Dimension(300, 35));
		via.setPreferredSize(new Dimension(300, 35));
		civico.setPreferredSize(new Dimension(70, 35));
		citta.setPreferredSize(new Dimension(300, 35));
		provincia.setPreferredSize(new Dimension(50, 35));
		cap.setPreferredSize(new Dimension(70, 35));
		cell.setPreferredSize(new Dimension(300, 35));
		tel.setPreferredSize(new Dimension(300, 35));
		mail.setPreferredSize(new Dimension(300, 35));
		tipoProfessionista.setPreferredSize(new Dimension(300, 35));

		nome.setBorder((BorderFactory.createEmptyBorder(4, 8, 4, 8)));
		cognome.setBorder((BorderFactory.createEmptyBorder(4, 8, 4, 8)));
		via.setBorder((BorderFactory.createEmptyBorder(4, 8, 4, 8)));
		civico.setBorder((BorderFactory.createEmptyBorder(4, 8, 4, 8)));
		citta.setBorder((BorderFactory.createEmptyBorder(4, 8, 4, 8)));
		provincia.setBorder((BorderFactory.createEmptyBorder(4, 8, 4, 8)));
		cap.setBorder((BorderFactory.createEmptyBorder(4, 8, 4, 8)));
		cell.setBorder((BorderFactory.createEmptyBorder(4, 8, 4, 8)));
		tel.setBorder((BorderFactory.createEmptyBorder(4, 8, 4, 8)));
		mail.setBorder((BorderFactory.createEmptyBorder(4, 8, 4, 8)));
		tipoProfessionista.setBorder((BorderFactory.createEmptyBorder(4, 8, 4,
				8)));

		nome.setOpaque(false);
		cognome.setOpaque(false);
		via.setOpaque(false);
		civico.setOpaque(false);
		citta.setOpaque(false);
		provincia.setOpaque(false);
		cap.setOpaque(false);
		cell.setOpaque(false);
		tel.setOpaque(false);
		mail.setOpaque(false);
		tipoProfessionista.setOpaque(false);

		nome.setFont(new Font("Arial", Font.PLAIN, 14));
		cognome.setFont(new Font("Arial", Font.PLAIN, 14));
		via.setFont(new Font("Arial", Font.PLAIN, 14));
		civico.setFont(new Font("Arial", Font.PLAIN, 14));
		citta.setFont(new Font("Arial", Font.PLAIN, 14));
		provincia.setFont(new Font("Arial", Font.PLAIN, 14));
		cap.setFont(new Font("Arial", Font.PLAIN, 14));
		cell.setFont(new Font("Arial", Font.PLAIN, 14));
		tel.setFont(new Font("Arial", Font.PLAIN, 14));
		mail.setFont(new Font("Arial", Font.PLAIN, 14));
		tipoProfessionista.setFont(new Font("Arial", Font.PLAIN, 14));
	}

	public String print() {
		String print = "";
		print = print + "#" + tipoProfessionista.getText();
		print = print + "\n#" + nome.getText();
		print = print + "\n#" + cognome.getText();
		print = print + "\n#" + via.getText();
		print = print + "\n#" + civico.getText();
		print = print + "\n#" + citta.getText();
		print = print + "\n#" + provincia.getText();
		print = print + "\n#" + cap.getText();
		print = print + "\n#" + cell.getText();
		print = print + "\n#" + tel.getText();
		print = print + "\n#" + mail.getText();
		return print;
	}

	public void setElement(String data) {
		// System.out.println("@@ " + data);
		Scanner c = new Scanner(data);
		String tmp = c.nextLine();

		try {
			tipoProfessionista.setText(tmp);
		} catch (NoSuchElementException e) {
			tipoProfessionista.setText("");
		}
		
		tmp = c.nextLine();
		try {
			nome.setText(tmp);
		} catch (NoSuchElementException e) {
			nome.setText("");
		}
		
		tmp = c.nextLine();
		try {
			cognome.setText(tmp);
		} catch (NoSuchElementException e) {
			cognome.setText("");
		}

		tmp = c.nextLine();

		try {
			via.setText(tmp);
		} catch (NoSuchElementException e) {
			via.setText("");
		}

		tmp = c.nextLine();

		try {
			civico.setText(tmp);
		} catch (NoSuchElementException e) {
			civico.setText("");
		}

		tmp = c.nextLine();
		try {
			citta.setText(tmp);
		} catch (NoSuchElementException e) {
			citta.setText("");
		}

		tmp = c.nextLine();
		try {
			provincia.setText(tmp);
		} catch (NoSuchElementException e) {
			provincia.setText("");
		}

		tmp = c.nextLine();
		try {
			cap.setText(tmp);
		} catch (NoSuchElementException e) {
			cap.setText("");
		}

		tmp = c.nextLine();
		try {
			cell.setText(tmp);
		} catch (NoSuchElementException e) {
			cell.setText("");
		}

		tmp = c.nextLine();
		try {
			tel.setText(tmp);
		} catch (NoSuchElementException e) {
			tel.setText("");
		}

		tmp = c.nextLine();
		try {
			mail.setText(tmp);
		} catch (NoSuchElementException e) {
			mail.setText("");
		}

	}

	

}
